<?php


namespace app\home\bussiness;



use app\common\model\Address;
use app\common\model\Cart;
use app\common\model\Spec;
use app\common\model\SpecGoods;
use app\home\model\User;

use think\facade\Db;
use think\facade\View;

class Goods
{
    /**
     *列表展示
     * @param $cate_id
     * @throws \think\db\exception\DbException
     */
    public static function list($cate_id){

        $data=\app\common\model\Goods::field('id,goods_name,goods_price,cate_id,goods_logo')->where('cate_id',$cate_id)->paginate(
            [
                'list_rows'=>10,
                'query'=>['cate_id'=>$cate_id],//query必须要数组形式,page不用写，模板自带了，具体看情况写
            ]
        );
        View::assign('data',$data);
    }

    /**
     * 商品详情
     * @param $goods_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function detail($goods_id){
        //联查
        $data=\app\common\model\Goods::with(['images','specGoods'])->find($goods_id)->toArray();
        $image=$data['images'];
        //dd($image);
        $data['goods_attr']=json_decode($data['goods_attr'],true);//这个超级重点，能把json的数组全部转化成正常的数组
        //dd($data);
        //获取规格值的id value_ids操作分割
        $spec=Spec::with('SpecValues')->where('type_id',$data['type_id'])->select()->toArray();
        //dd($spec);
        //猜你喜欢列表
        //跟当前购物车中分类同属的4件热销商品
        $cate_id=$data['cate_id'];
        $hot=\app\common\model\Goods::where('cate_id',$cate_id)
            ->where('is_hot','=',1)
            ->limit(4)
            ->select();
        foreach ($hot as $val){
            $val->goods_attr=json_encode($val->goods_attr,true);
        }

        View::assign('hot',$hot);
        View::assign('data',$data);
        View::assign('image',$image);
        View::assign('spec',$spec);
    }

    /**
     * 添加到购物车表并且返回相关数据
     * @param $goods_id
     * @param $value_ids
     * @param $num
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function success_cart($goods_id,$value_ids,$num){
        //查询数据，返回商品名称，规格值，商品logo,商品数量
        // spec——goods
        // goods表
        $data=\app\common\model\Goods::
        hasWhere('specGoods',function ($data){
           $data->column('value_ids,value_names,price');
        })
            ->where('pyg_goods.id',$goods_id)
            ->where('value_ids',$value_ids)
            ->column('pyg_goods.id,goods_name,goods_logo,value_ids,value_names,price,specGoods.id');

        $data=$data[0];
        $data['num']=$num;
        $spec_goods_id=SpecGoods::where('goods_id',$goods_id)
            ->where('value_ids',$value_ids)
            ->value('id');
        $data['spec_goods_id']=$spec_goods_id;
            $user_id=User::where('username',session('account'))->value('id');
            //判断是否有相同的购物记录
            $where=[
                'user_id'=>$user_id,
                'goods_id'=>$goods_id,
                'spec_goods_id'=>$data['spec_goods_id']
            ];
            $res=Cart::where($where)->find();
            if($res){
                //累加数量修改表
                    //这里有个小点，限量并且要看有没有库存了
                Cart::where('id',$res['id'])->update(['number'=>$res['number']+=$num]);
            }else{
                //添加到购物车表
                $cart=[
                    'id'=>null,
                    'user_id'=>$user_id,
                    'goods_id'=>$goods_id,
                    'number'=>$data['num'],
                    'spec_goods_id'=>$data['spec_goods_id'],
                    'is_selected'=>0
                ];
               Cart::create($cart);
            }
            View::assign('data',$data);
    }


    /**
     * 购物车列表展示
     */
    public static function cart(){
        //联查，找goods，spec_goods

        $data=Cart::select()->toArray();
        foreach ($data as $val){
            $goods=\app\common\model\Goods::field('goods_logo,goods_name')->where('id',$val['goods_id'])->find()->toArray();
            $spec_goods=SpecGoods::field('price,value_names')->where('id',$val['spec_goods_id'])->find()->toArray();
            $list[]=[
                'is_selected'=>$val['is_selected'],
                'cart_id'=>$val['id'],
                'goods_logo'=>$goods['goods_logo'],
                'goods_name'=>$goods['goods_name'],
                'value_names'=>$spec_goods['value_names'],
                'price'=>$spec_goods['price'],
                'num'=>$val['number']
            ];
            $goods_id[]=$val['goods_id'];
        }
        //查出所有购物车的商品id
       $goods_id=array_unique($goods_id);
        //找出所有对应的分类id
        $cate_id=\app\common\model\Goods::where('id','in',$goods_id)->column('cate_id');
        //找四个热销产品
        $hot=\app\common\model\Goods::where('cate_id','in',$cate_id)
            ->where('is_hot','=',1)
            ->limit(4)
            ->select();
        foreach ($hot as $val){
            $val->goods_attr=json_encode($val->goods_attr,true);
        }
        View::assign('hot',$hot);
        View::assign('list',$list);

    }


    /**
     * 修改购物车选中的状态
     * @param $data
     */
    public static function changeStatus($data){
        if($data['id']=='all'){
            Cart::where('is_selected','<>',$data['status'])->update(['is_selected'=>$data['status']]);
        }else{
            Cart::where('id',$data['id'])->update(['is_selected'=>$data['status']]);
        }

    }

    /**
     * 修改购物车中商品的数量
     *
     */
    public static function changeNumber($data){
        Cart::where('id',$data['id'])->update(['number'=>$data['number']]);
    }


    /**
     * 收件人信息列表展示
     */
    public static function address(){
        $user_name=session('account');//用户名唯一
        $uid=\app\common\model\User::where('username',$user_name)->value('id');
        $address=Address::where('user_id',$uid)->select()->toArray();
         View::assign('address',$address);
        return $uid;
    }

    /**
     * 选中商品的列表信息
     */
    public static function goods(){
        //购物车id，商品logo，商品名称加属性值，商品单价，商品数量，库存状况
        $cart=Cart::where('is_selected','=',1)->select()->toArray();
        foreach ($cart as $val){
            $goods=\app\common\model\Goods::field(['goods_name','goods_logo'])->find($val['goods_id'])->toArray();
            $spec_goods=SpecGoods::find($val['spec_goods_id']);
            //dd($goods);
            //判断库存状况，有货或者没货
                //库存-冻结库存不能小于数量
            if($spec_goods['store_count']-$spec_goods['store_frozen']<$val['number']){
                $store='库存不足';
            }else{
                $store='有货';
            }
            $list[]=[
                'id'=>$val['id'],
                'goods_name'=>$goods['goods_name'],
                'goods_logo'=>$goods['goods_logo'],
                'attr'=>$spec_goods['value_names'],
                'price'=>$spec_goods['price'],
                'number'=>$val['number'],
                'store'=>$store
            ];
        }
        $total_price=0;
        $total_number=0;
        foreach ($list as $val){
            $total_price+=$val['price'];
            $total_number+=$val['number'];
        }
        View::assign('total_price',$total_price);
        View::assign('total_number',$total_number);
        View::assign('list',$list);
    }

    /**
     * 生成订单
     */
    public static function order($uid){
        try {
            $order_sn=time().rand(1000,9999);

            //开启事务
            Db::startTrans();
            //添加order表

            //添加order——goods表

            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }

    }

}